using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace GotLyrics
{
#if WhatWasIThinking

    /// <summary>
    /// This is the base class to grab lyrics from websites by scraping an html result. 
    /// </summary>
    public abstract class LyricScrapeGrabber : ILyricGrabber
    {
        #region ILyricGrabber Members

        private bool _enabled = true;
        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ILyricGrabber"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        /// <summary>
        /// The website of this lyrics provider. This is pointing to the main page on the website.
        /// </summary>
        public abstract string WebSiteUrl { get; }

        private LyricGrabberContext _context;
        /// <summary>
        /// Gets the context. Use this property to retrieve information from the current mp3 file.
        /// </summary>
        /// <value>The context.</value>
        public LyricGrabberContext Context
        {
            get { return _context; }
        }
	
        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns>The lyrics it found.</returns>
        public string ProcessRequest(LyricGrabberContext context)
        {
            _context = context;

            if (!Enabled) return null;

            string result = null;

            string requestAddress = FormatRequestAddress();

            if (!string.IsNullOrEmpty(requestAddress))
            {
                string response = PerformRequest(requestAddress);

                if (!string.IsNullOrEmpty(response))
                    result = ParseResponse(response);
            }

            return result;
        }

        #endregion

        private WebClient _wc;
        /// <summary>
        /// Gets the web client.
        /// </summary>
        /// <value>The web client.</value>
        protected WebClient WebClient
        {
            get
            {
                if (_wc == null)
                {
                    _wc = new WebClient();
                    _wc.Encoding = Encoding.UTF8;
                }

                return _wc;
            }
        }

        /// <summary>
        /// Formats the request address.
        /// </summary>
        /// <remarks>
        /// Use the <see cref="Context"/> property to access tag information if you inherit from this class.
        /// </remarks>
        /// <returns>The formated URL where to get the lyric from.</returns>
        protected abstract string FormatRequestAddress();

        /// <summary>
        /// Performs the request.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns>The response of the website.</returns>
        /// <remarks>If you override this function, make sure that the encoding of the returned value is correct.</remarks>
        protected virtual string PerformRequest(string url)
        {
            return WebClient.DownloadString(url);
        }

        /// <summary>
        /// Parses the response from the website.
        /// </summary>
        /// <param name="response">The response from the website.</param>
        /// <returns>The parsed (scraped) lyric.</returns>
        /// <remarks>Return <c>null</c> or an empty string if the scrape wasn't able to locate the lyrics.</remarks>
        protected abstract string ParseResponse(string response);
    }
#endif
}
